<?php

/**
 * Created on May 16.
 *  * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * 		   Stephan Beisken (beisken@ebi.ac.uk)
 * 		   Luis Felipe De Figueiredo (ldpf@ebi.ac.uk)
 */
class BondAtom implements iCRUD {
    /*
     * Attributes
     */

    private $_bondID;
 // Foreign key
    private $_atomID;
 // Foreign key
    private $_orderNumber;



	public function BondAtom(){
		$this->_bondID = null;
        $this->_atomID = null;
        $this->_orderNumber = null;
	}
    /*
     * Constructor
     */
    public function __BondAtom($orderNumber, $bondID, $atomID) {

        $this->_bondID = $bondID;
        $this->_atomID = $atomID;
        $this->_orderNumber = $orderNumber;
    }

    /*
     * Accessors
     */

    public function get_bondID() {
        return $this->_bondID;
    }

    public function get_atomID() {
        return $this->_atomID;
    }

    public function get_orderNumber() {
        return $this->_orderNumber;
    }

    /*
     * Modifiers
     */

    public function set_bondID($_bondID) {
        $this->_bondID = $_bondID;
    }

    public function set_atomID($_atomID) {
        $this->_atomID = $_atomID;
    }

    public function set_orderNumber($_orderNumber) {
        $this->_orderNumber = $_orderNumber;
    }

    /*
     * CRUD
     */

    /**
     * Put an object in the database
     */
    public function create() {

        $connection=Database::connect();
            $query = "INSERT INTO `BOND_ATOM` values ('".
            		$this->_bondID . "','" .
                    $this->_atomID . "','" .
                    $this->_orderNumber . "')";
            mysql_query($query) or die(mysql_error());     
        Database::disconnect($connection);
    }

	public  static function createFromArray($arrayOfBondAtom){
		$connection=Database::connect();
		foreach($arrayOfBondAtom as $bondatom){
			$query = "INSERT INTO `BOND_ATOM` values ('".
			$bondatom->_bondID . "','" .
			$bondatom->_atomID . "','" .
			$bondatom->_orderNumber . "')";
            mysql_query($query) or die(mysql_error());   
		}
		Database::disconnect($connection);
	}


    /**
     * Read an object from the database
     */
    public static function read($key) {

        $connection=Database::connect();
        $query = "SELECT * FROM BOND_ATOM WHERE bond_id = '" . $key[0] . "'
             AND atom_id = '" . $key[1] . "'";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        $bondAtom=new BondAtom();
        $bondAtonm->__BondAtom($bond_id, $atom_id, $order_number);
		Database::disconnect($connection);
        return $bondAtom;
    }

    /*
     * Update an object from the database
     */

    public function update() {

        $connection=Database::connect();
        $query = "UPDATE `BOND_ATOM` SET order_number='" .
                $this->_orderNumber . "' WHERE bond_id='" .
                $this->_bondID . "' AND atom_id='" .
                $this->_atomID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }

    /*
     * Delete an object from the database
     */

    public function delete() {

        $connection=Database::connect();
        $query = "DELETE FROM `BOND_ATOM` WHERE bond_id='" . $this->_bondID .
                "'AND atom_id='" . $this->_atomID . "'";
        mysql_query($query) or die(mysql_error());
        Database::disconnect($connection);
    }
    
    /*
     * 
     * All other methods to query the database
     * 
     */
    
    
    public static function getAllBondsFromAtoms($arrayOfAtoms) {
    	
    	$file="Test";
		if(class_exists($file)){
			echo "the class $file exists\n</br></br>";
		}else{
			echo "the class $file does not exists";
		}
    	
    	$arrayOfAtomId=array();
    	$arrayOfBonds=array();
    	$connection=Database::connect();
    	foreach($arrayOfAtoms as $key => $value){
    		array_push($arrayOfAtomId,$value[0]->getAtomID());
    	}
		$stringArray=implode("','",$arrayOfAtomId);
    	$query="select * from BOND_ATOM where atom_id in ('".$stringArray."') order by bond_id";
    	$arrayOfBondAtom=mysql_query($query);
    	$previousId=-1;
    	$c=0;
    	$arrayOfReferenceAtoms=array();
		while($row=mysql_fetch_object($arrayOfBondAtom)){
//			echo $row->bond_id.", ".$row->atom_id."</br>";
			array_push($arrayOfReferenceAtoms,$row->atom_id);
			$id=$row->bond_id;
//			echo $id." ; ";
			$object=new BondAtom();
//			var_dump($row->order_number." ; ".$row_->bond_id." ; ".$row->atom_id);
			$object->__BondAtom($row->order_number,$row->bond_id ,$row->atom_id);
			if($previousId!=$id && $c>0){
				array_push($arrayOfReferenceAtoms,$row->atom_id);
				$objectBond=Bond::read($id);
				array_push($arrayOfBonds,array($objectBond,$arrayOfReferenceAtoms));
//				var_dump("So far the array of bonds  contains ".$arrayOfBonds->size()."</br>");				
				$arrayOfReferenceAtoms=array();
//				echo $objectBond->get_bondID()." ; ".$objectBond->get_stereo()."</br>";
			}
			$previousId=$id;
			$c++;
		}
    	Database::disconnect($connection);
    	return $arrayOfBonds;    	
    }
    
    
    

}
?>
